package com.tql.kingdee.service;

import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.datasource.annotation.Slave;
import com.tql.kingdee.bean.TqlStapPriceBean;
import com.tql.kingdee.mapper.MaterialIncaseMapper;
import com.tql.kingdee.response.MaterialIncaseResponse;
import com.tql.kingdee.util.CommonUtil;
import com.tql.kingdee.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service

public class MaterialIncaseService {

    @Autowired
    private MaterialIncaseMapper materialIncaseMapper;

    @Autowired
    private DsTestService dsTestService;

    @Slave
    public int batchAddIncase(List<MaterialIncaseResponse> list, int month) throws ParseException {

        String tabname = CommonUtil.TABLENAME_TQL_MATARIAL_INCASE+month;
        int a = materialIncaseMapper.batchAdd(list,tabname);
        return a;
    }
    @Master
    public void batchAddStapPrice(List<MaterialIncaseResponse> list)
        {
            List<TqlStapPriceBean> batchInsert = new ArrayList<>();
            for (MaterialIncaseResponse incase : list) {
                TqlStapPriceBean tqlStapPrice = new TqlStapPriceBean();
                tqlStapPrice.setStapCode(incase.getFMaterialIdfnumber());
                tqlStapPrice.setStapName(incase.getFMaterialIdfname());
                tqlStapPrice.setLastPrice(incase.getFPrice());
                tqlStapPrice.setWeightedPrice(incase.getFPrice());
                if (StringUtils.isNotBlank(incase.getFStockIdfnumber()) && !incase.getFStockIdfnumber().startsWith("BU")){
                    incase.setFStockIdfnumber(CommonUtil.deptCodeReplacePrefix(incase.getFStockIdfnumber()));
                }
                tqlStapPrice.setDeptCode(incase.getFStockIdfnumber());
                batchInsert.add(tqlStapPrice);
            }
            if (!batchInsert.isEmpty()) {
                this.insertBatchTqlStapPrice(batchInsert);
            }
        }
        @Slave
        public void batchAddInchange(List<MaterialIncaseResponse> list, int month)
{
    if (!list.isEmpty()) {
        String inChangeTabName = CommonUtil.TQL_STAP_IN_CHANGE+month;
        dsTestService.batchAddIncase(list,inChangeTabName, 1);
    }
}


    @Master
    public void insertBatchTqlStapPrice(List<TqlStapPriceBean> batchInsert) {
        materialIncaseMapper.insertBatchTqlStapPrice(batchInsert);
    }

    @Slave
    public List<MaterialIncaseResponse> selectList(String tabname) {
       return materialIncaseMapper.selectList(tabname);
    }
}
